﻿using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using FangChanCrmModel;
using Maticsoft.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace FangChanCrm.Services.Realization
{
    public class t_GongGaoBiaoService : It_GongGaoBiaoService
    {
        public readonly MyContext _Context;

        public t_GongGaoBiaoService(MyContext context)
        {
            _Context = context;
        }

        public t_GongGaoBiao GetId(int id)
        {
            return  _Context.t_GongGaoBiao.FirstOrDefault(x => x.Id == id);
        }
        public List<t_GongGaoBiao> GetList()
        {
            return _Context.t_GongGaoBiao.ToList();
        }
        public string GetListWithFenYe(t_GongGaoBiao t_GongGaoBiao, int start, int limit, string dir, int column)
        {
            var returnwhere = ReturnWhere(t_GongGaoBiao);
            var list = new List<t_GongGaoBiao>();
            if (returnwhere.Length > 0)
            {
                list = _Context.t_GongGaoBiao.Where(returnwhere).ToList();
            }
            else
            {
                list = _Context.t_GongGaoBiao.ToList();
            }
            switch (column)
            {
                case 0:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.GongGaoNeiRong).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.GongGaoNeiRong).ToList();
                    }
                    break;
                case 1:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.BeiZhu1).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.BeiZhu1).ToList();
                    }
                    break;
                default:
                    break;
            }
            return Helper.ListToJsonAddFenYe(list, start, limit);
        }

        public string GongGaoAdd(t_GongGaoBiao GongGao)
        {
            _Context.t_GongGaoBiao.Add(GongGao);
            if (_Context.SaveChanges() > 0)
            {
                return "success";
            }
            else
            {
                return "error";
            }
        }

        public string ReturnWhere(t_GongGaoBiao t_GongGaoBiao)
        {
            string returnstr = "Id != 1";
            if (t_GongGaoBiao.BeiZhu1 != null)
            {
                returnstr = returnstr + " and BeiZhu1.Contains(\"" + t_GongGaoBiao.BeiZhu1 + "\")";
            }
            if (t_GongGaoBiao.BeiZhu2 != null)
            {
                returnstr = returnstr + " and BeiZhu2.Contains(\"" + t_GongGaoBiao.BeiZhu2 + "\")";
            }
            if (t_GongGaoBiao.BeiZhu3 != null)
            {
                returnstr = returnstr + " and BeiZhu3.Contains(\"" + t_GongGaoBiao.BeiZhu3 + "\")";
            }
            if (t_GongGaoBiao.BeiZhu4 != null)
            {
                returnstr = returnstr + " and BeiZhu4.Contains(\"" + t_GongGaoBiao.BeiZhu4 + "\")";
            }
            if (t_GongGaoBiao.BeiZhu5 != null)
            {
                returnstr = returnstr + " and BeiZhu5.Contains(\"" + t_GongGaoBiao.BeiZhu5 + "\")";
            }
            if (t_GongGaoBiao.BeiZhu6 != null)
            {
                returnstr = returnstr + " and BeiZhu6.Contains(\"" + t_GongGaoBiao.BeiZhu6 + "\")";
            }
            if (t_GongGaoBiao.BeiZhu7 != null)
            {
                returnstr = returnstr + " and BeiZhu7.Contains(\"" + t_GongGaoBiao.BeiZhu7 + "\")";
            }
            if (t_GongGaoBiao.BeiZhu8 != null)
            {
                returnstr = returnstr + " and BeiZhu8.Contains(\"" + t_GongGaoBiao.BeiZhu8 + "\")";
            }
            if (t_GongGaoBiao.BeiZhu9 != null)
            {
                returnstr = returnstr + " and BeiZhu9.Contains(\"" + t_GongGaoBiao.BeiZhu9 + "\")";
            }
            if (t_GongGaoBiao.BeiZhu10 != null)
            {
                returnstr = returnstr + " and BeiZhu10.Contains(\"" + t_GongGaoBiao.BeiZhu10 + "\")";
            }
            if (t_GongGaoBiao.FangBuRenId != 0)
            {
                returnstr = returnstr + " and FangBuRenId ==(" + t_GongGaoBiao.FangBuRenId + ")";
            }
            if (t_GongGaoBiao.GongGaoNeiRong != null)
            {
                returnstr = returnstr + " and GongGaoNeiRong.Contains(\"" + t_GongGaoBiao.GongGaoNeiRong + "\")";
            }
            return returnstr;
        }

        
    }
}
